package com.softroad.help.app.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.softroad.help.app.R;
import com.softroad.help.app.ui.MainActivity.OnClick;

public class MainAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private Resources resources;
	private OnClick onClick;
	private Integer[] data = { R.drawable.tab1, R.drawable.tab2,
			R.drawable.tab3, R.drawable.tab4,
			R.drawable.tab5, R.drawable.tab6 };
	private Integer[] txt = { R.string.tab1, R.string.tab2, R.string.tab3,
			R.string.tab4, R.string.tab5, R.string.tab6 };

	public MainAdapter(Context context, OnClick click) {
		this.inflater = LayoutInflater.from(context);
		this.resources = context.getResources();
		this.onClick = click;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.length;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return data[arg0];
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int position, View contentView,
			ViewGroup viewGroup) {
		ViewHolder holder = null;
		if (contentView == null) {
			contentView = inflater.inflate(R.layout.adapter_main, null);
			holder = new ViewHolder();
			holder.imageView = (ImageView) contentView.findViewById(R.id.img);
			holder.textView = (TextView) contentView.findViewById(R.id.txt);
			contentView.setTag(holder);
		} else {
			holder = (ViewHolder) contentView.getTag();
		}
		holder.imageView.setBackgroundResource(data[position]);
		holder.textView.setText(resources.getString(txt[position]));
		holder.imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				onClick.onClick(position);
			}
		});
		return contentView;
	}

	class ViewHolder {
		private ImageView imageView;
		private TextView textView;
	}

}
